/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/5/22.
//
#include <unordered_set>

#include "sql/stmt/filter_stmt.h"
#include "common/log/log.h"
#include "sql/stmt/update_stmt.h"
#include "storage/common/db.h"
#include "storage/common/table.h"
#include "util/util.h"

UpdateStmt::UpdateStmt(Table *table, const Value *values, int value_amount, FilterStmt *filter_stmt)
  : table_ (table), values_(values), value_amount_(value_amount), filter_stmt_(filter_stmt)
{}

UpdateStmt::~UpdateStmt()
{
  if (nullptr != filter_stmt_) {
    delete filter_stmt_;
    filter_stmt_ = nullptr;
  } 
}

RC UpdateStmt::create(Db *db, const Updates &update_sql, Stmt *&stmt)
{
  const char *table_name = update_sql.relation_name;
  if (nullptr == db || nullptr == table_name) {
    LOG_WARN("invalid argument. db=%p, table_name=%p", 
             db, table_name);
    return RC::INVALID_ARGUMENT;
  }

  // check whether the table exists
  Table *table = db->find_table(table_name);
  if (nullptr == table) {
    LOG_WARN("no such table. db=%s, table_name=%s", db->name(), table_name);
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }
  const TableMeta &table_meta = table->table_meta();


  std::vector<FieldMeta> update_field_metes;
  const Value *values = &(update_sql.value);
  const int value_num = 1;
  
  std::unordered_set<std::string> field_set;
  for (int i = 0; i < value_num; i++) {
    // TODO: 可能会有多个属性需要更新，这里attribute_name应该改为数组
    const char *field_name = update_sql.attribute_name;
    // check fields type
    const FieldMeta *field_meta = table_meta.field(field_name);
    if (nullptr == field_meta) {
      LOG_WARN("no such field. field=%s.%s.%s", db->name(), table->name(), field_name);
      return RC::SCHEMA_FIELD_MISSING;
    }
    if (field_set.count(field_meta->name()) != 0) {
      LOG_WARN("duplicate field in update sql. field=%s.%s.%s", db->name(), table->name(), field_name);
      return RC::SCHEMA_FIELD_MISSING;
    }
    field_set.insert(field_meta->name());

    const AttrType field_type = field_meta->type();

    const AttrType value_type = values[i].type;
    if (field_type != value_type) { // try to convert the value type to field type
      RC rc = convert_value_type(field_type, value_type, const_cast<Value &>(values[i]));
      if (rc != SUCCESS) {
        LOG_WARN("field type misFmatch. table=%s, field=%s, field type=%d, value_type=%d", 
          table_name, field_meta->name(), field_type, value_type);
        return RC::SCHEMA_FIELD_TYPE_MISMATCH;
      }
    }
    update_field_metes.push_back(*field_meta);

  }
  std::unordered_map<std::string, Table *> table_map;
  table_map.insert(std::pair<std::string, Table *>(std::string(table_name), table));

  FilterStmt *filter_stmt = nullptr;
  RC rc = FilterStmt::create(db, table, &table_map,
			     update_sql.conditions, update_sql.condition_num, filter_stmt);
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to create filter statement. rc=%d:%s", rc, strrc(rc));
    return rc;
  }

  // TODO
  UpdateStmt *update_stmt = new UpdateStmt(table, values, value_num, filter_stmt);
  update_stmt->update_fields_.swap(update_field_metes);
  stmt = update_stmt;
  return RC::SUCCESS;
}
